﻿using System;

namespace BankTransfer.Bank.Types.Types
{
    /// <summary>
    /// 审计消息
    /// </summary>
    public class AuditMessage
    {
        public UserId UserId { get; private set; }
        public AccountNumber Source { get; private set; }
        public AccountNumber Target { get; private set; }
        public Money Money { get; private set; }
        public DateTime Date { get; private set; }
        public AuditMessage(UserId userId, AccountNumber source, AccountNumber target, Money money)
        {
            UserId = userId;
            Source = source;
            Target = target;
            Money = money;
            Date = DateTime.Now;
        }
        public AuditMessage(UserId userId, AccountNumber source, AccountNumber target, Money money, DateTime date)
        {
            UserId = userId;
            Source = source;
            Target = target;
            Money = money;
            Date = date;
        }
        public string Serialize()
        {
            return UserId.Value + "," + Source.Value + "," + Target.Value + "," + Money.Amount + "," + Date.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public static AuditMessage Deserialize(string value)
        {
            var arr = value?.Split(new char[] { ',' });
            if (arr?.Length != 5)
            {
                throw new ArgumentException("AuditMessage 格式不正确！");
            }
            return new AuditMessage(new UserId(arr[0]), new AccountNumber(arr[2]), new AccountNumber(arr[3]), new Money(arr[4]), DateTime.Parse(arr[5]));
        }
    }
}